﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Net;
using System.IO;
using RestSharp;
using EasyClassUI.entity;

namespace EasyClassUI.WebRequest {
    class LoginStatus {
        public string StatusCode { get; set; }
        public string Token { get; set; }
    }

    class AccountService {
        private string page = "https://localhost:5001/user/";
        public AccountService() {

        }

        public string Register(User newUser) {
            string RegisterJson = newUser.exportRegisterJson();
            var client = new RestClient($"{page}register");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", RegisterJson, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }

        public LoginStatus Login(User user) {
            string url = $"{page}login?UserAccount={user.UserAccount}&Password={user.Password}";
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddParameter("text/plain", "", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            string Token = response.Content.ToString();
            Token = Token.Replace("\"", "");
            return new LoginStatus { StatusCode = status, Token = Token };
        }
    }
}
